package com.ywu.application.standard.container.config;

import com.ywu.common.gson.GsonUtils;
import com.ywu.application.standard.container.entity.SshConfigEntity;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName AppConfig
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/5 19:47
 * @Version 1.0
 **/
@Data
@ToString
@Configuration
@ConfigurationProperties(prefix = "ywu.container")
public class YwuContainerAppConfig {
    private String sshHost;

    private Integer sshPort;

    private String sshUserName;

    private String sshPassword;

    private String sshConfig;

    private Map<String, SshConfigEntity> configEntityMap;

    private String mavenPath;

    private String frpcFilePath;

    private String frpcFileName;

    private String frpcLocalFilePath;

    private String jarLocalFilePath;

    private String appLogPath = "/appfile/logs/";

    private String dockerJarPath = "/app/";

    public SshConfigEntity getSshConfig(String ip) {
        if (Objects.isNull(configEntityMap)) {
            synchronized (this) {
                if (Objects.isNull(configEntityMap)) {
                    List<SshConfigEntity> sshConfigEntities = GsonUtils.deserializeList(this.sshConfig, SshConfigEntity.class);
                    configEntityMap = sshConfigEntities.stream()
                            .collect(Collectors.toMap(SshConfigEntity::getSshHost, SshConfigEntity -> SshConfigEntity, (v1, v2) -> v1));
                }
            }
        }
        return configEntityMap.getOrDefault(ip, null);
    }
}
